//
// Created by pzw on 24-1-4.
//

#include "CxxHandler.h"

CxxMessage::CxxMessage() = default;

CxxMessage::~CxxMessage() = default;

/**初始化对象池实例*/
MessagePool CxxMessage::messagePool{};

CxxMessage *CxxMessage::obtain() {
    CxxMessage *message = messagePool.acquire();
    message->what = 0;
    message->handler = nullptr;
    message->callBack = nullptr;
    return message;
}

CxxMessage *CxxMessage::obtain(int what, CxxHandler *pHandler) {
    CxxMessage *message = messagePool.acquire();
    message->what = what;
    message->handler = pHandler;
    message->callBack = nullptr;
    return message;
}

void CxxMessage::recycle(CxxMessage *pMessage) {
    pMessage->what = 0;
    pMessage->handler = nullptr;
    pMessage->callBack = nullptr;
    /*在回收消息时，将上一次消息压入栈中，达到重复利用 @{*/
    messagePool.release(pMessage);
}

CxxMessage *MessagePool::acquire() {
    if (pool.empty()) {
        return new CxxMessage();
    } else {
        CxxMessage *message = pool.back();
        pool.pop_back();
        return message;
    }
}

void MessagePool::release(CxxMessage *message) {
    pool.push_back(message);
    /*在回收消息时，将上一次消息压入栈中，达到重复利用 @}*/
}

